package com.koo.framework.query;

import com.koo.common.exception.RRException;
import org.apache.commons.beanutils.BeanUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Condition implements Serializable {
    private String key;
    private Object value;
    private OpType op;
    private String opStr;
    private RelateType relateType;
    private String relateStr;

    public Condition() {
        this.relateType = Condition.RelateType.AND;
    }

    public Condition(String key, String value, OpType op) {
        this.key = key;
        this.value = value;
        this.op = op;
        this.opStr = getSign(op);
        this.relateType = Condition.RelateType.AND;
    }

    public Condition(String key, String value, OpType op, RelateType relateType) {
        this.key = key;
        this.value = value;
        this.op = op;
        this.relateType = relateType;
        this.opStr = getSign(op);
        this.relateType = relateType;
        this.relateStr = getRelateStr(relateType);
    }

    public static Map createEqMap(String keyName, String keyValue) {
        Map<String, Object> map = new HashMap();
        List<Condition> whereList = new ArrayList();
        whereList.add(createEq(keyName, keyValue));
        map.put("whereList", whereList);
        return map;
    }

    public static Map createMap(String[] keys, String[] values, String[] ops) {
        Map<String, Object> map = new HashMap();
        List<Condition> whereList = new ArrayList();
        if (values != null && keys != null && values.length == keys.length) {
            int i;
            if (ops == null) {
                for(i = 0; i < keys.length; ++i) {
                    whereList.add(createEq(keys[i], values[i]));
                }
            } else if (ops.length == values.length) {
                for(i = 0; i < keys.length; ++i) {
                    if ("=".equals(ops[i])) {
                        whereList.add(createEq(keys[i], values[i]));
                    } else if (">=".equals(ops[i])) {
                        whereList.add(create(keys[i], values[i], Condition.OpType.GE));
                    } else if (">".equals(ops[i])) {
                        whereList.add(create(keys[i], values[i], Condition.OpType.GT));
                    } else if ("<=".equals(ops[i])) {
                        whereList.add(create(keys[i], values[i], Condition.OpType.LE));
                    } else if ("<".equals(ops[i])) {
                        whereList.add(create(keys[i], values[i], Condition.OpType.LT));
                    } else if ("!=".equals(ops[i])) {
                        whereList.add(create(keys[i], values[i], Condition.OpType.NE));
                    } else if ("in".equals(ops[i])) {
                        whereList.add(create(keys[i], values[i], Condition.OpType.IN));
                    } else if ("like".equals(ops[i])) {
                        whereList.add(create(keys[i], values[i], Condition.OpType.LK));
                    }
                }
            }
        }

        map.put("whereList", whereList);
        return map;
    }

    public static Map createEQMap(String[] keys, String[] values) {
        Map<String, Object> map = new HashMap();
        List<Condition> whereList = new ArrayList();
        if (values != null && keys != null && values.length == keys.length) {
            for(int i = 0; i < keys.length; ++i) {
                whereList.add(createEq(keys[i], values[i]));
            }
        }

        map.put("whereList", whereList);
        return map;
    }

    public static Map createLikeMap(String keyName, String keyValue) {
        Map<String, Object> map = new HashMap();
        List<Condition> whereList = new ArrayList();
        whereList.add(createLike(keyName, keyValue));
        map.put("whereList", whereList);
        return map;
    }

    public static Map createNotEqualMap(String keyName, String keyValue) {
        Map<String, Object> map = new HashMap();
        List<Condition> whereList = new ArrayList();
        whereList.add(createNotEqual(keyName, keyValue));
        map.put("whereList", whereList);
        return map;
    }

    public String getKey() {
        return this.key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public Object getValue() {
        return this.value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public OpType getOp() {
        return this.op;
    }

    public void setOp(OpType op) {
        this.op = op;
    }

    public String getOpStr() {
        return getSign(this.op);
    }

    public void setOpStr(String opStr) {
        this.opStr = opStr;
    }

    public RelateType getRelateType() {
        return this.relateType;
    }

    public void setRelateType(RelateType relateType) {
        this.relateType = relateType;
    }

    public void setRelateStr(String relateStr) {
        this.relateStr = relateStr;
    }

    public String getRelateStr() {
        return getRelateStr(this.relateType);
    }

    public static String getRelateStr(RelateType relateType) {
        String relateStr = "";
        switch (relateType) {
            case OR:
                relateStr = "or";
                break;
            case AND:
                relateStr = "and";
                break;
            default:
                relateStr = "and";
        }

        return relateStr;
    }

    public static String getSign(OpType op) {
        String sign = "";
        switch (op) {
            case EQ:
                sign = "=";
                break;
            case GE:
                sign = ">=";
                break;
            case LE:
                sign = "<=";
                break;
            case GT:
                sign = ">";
                break;
            case LT:
                sign = "<";
                break;
            case LK:
                sign = "like";
                break;
            case IN:
                sign = "in";
                break;
            case NE:
                sign = "<>";
                break;
            case OT:
                sign = "other";
                break;
            default:
                sign = "=";
        }

        return sign;
    }

    public static Condition createEq(String key, Object value) {
        Condition model = new Condition();
        model.key = key;
        model.op = Condition.OpType.EQ;
        model.value = getBeanValue(key, value);
        model.opStr = getSign(Condition.OpType.EQ);
        model.relateType = Condition.RelateType.AND;
        return model;
    }

    public static Condition createLike(String key, Object value) {
        Condition model = new Condition();
        model.key = key;
        model.op = Condition.OpType.LK;
        model.value = getBeanValue(key, value);
        model.opStr = getSign(Condition.OpType.LK);
        model.relateType = Condition.RelateType.AND;
        return model;
    }

    public static Condition createNotEqual(String key, Object value) {
        Condition model = new Condition();
        model.key = key;
        model.op = Condition.OpType.NE;
        model.value = getBeanValue(key, value);
        model.opStr = getSign(Condition.OpType.NE);
        model.relateType = Condition.RelateType.AND;
        return model;
    }

    public static Condition create(String key, Object value, OpType op) {
        Condition model = new Condition();
        model.key = key;
        model.value = getBeanValue(key, value);
        model.op = op;
        model.opStr = getSign(op);
        model.relateType = Condition.RelateType.AND;
        return model;
    }

    public static Condition create(String key, Object value, OpType op, RelateType relateType) {
        Condition model = new Condition();
        model.key = key;
        model.value = getBeanValue(key, value);
        model.op = op;
        model.opStr = getSign(op);
        model.relateType = relateType;
        model.relateStr = getRelateStr(relateType);
        return model;
    }

    public static Object getBeanValue(String key, Object value) {
        if (value != null) {
            String valueClass = value.getClass().getName();
            if (valueClass.indexOf("com.asdzy.purpleredgourd") > -1) {
                try {
                    value = BeanUtils.getProperty(value, key);
                } catch (Exception var4) {
                    throw new RRException(value + "获取参数失败:" + key);
                }
            }
        }
        return value;
    }

    public static enum RelateType {
        AND("AND"),
        OR("OR");

        private String value;

        private RelateType(String value) {
            this.value = value;
        }

        public String getValue() {
            return this.value;
        }

        public String toString() {
            return String.valueOf(this.value);
        }
    }

    public static enum OpType implements Serializable {
        EQ("EQ"),
        GE("GE"),
        LE("LE"),
        GT("GT"),
        LT("LT"),
        LK("LK"),
        IN("IN"),
        NE("NE"),
        OT("OT");

        private String value;

        private OpType(String value) {
            this.value = value;
        }

        public String getValue() {
            return this.value;
        }

        public String toString() {
            return String.valueOf(this.value);
        }
    }
}
